package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.user.personal.AccountSecurityFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.UdaUtils;

/**
 * Created by Administrator on 2016/7/7.
 */
public class AccountSecurityActivity extends AbsTitleBarActivity {

    public static void start(Context context) {
        context.startActivity(new Intent(context, AccountSecurityActivity.class));
    }

    public static void start(Context context, PbBean.PbUserInfo info) {
        Intent intent = new Intent(context, AccountSecurityActivity.class);
        intent.putExtra("user_info", info);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        String title = "账号与安全";
        return title;
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new AccountSecurityFragment();
    }

    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putSerializable("user_info", getIntent().getSerializableExtra("user_info"));
        return args;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            FragmentManager manager = getSupportFragmentManager();
            int count = manager.getBackStackEntryCount();
            if(count == 0) {
                showConfirm();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onHeadBackClick(View view) {
        //        EventBusUtil.post(new MessageNoticeEvent(MessageNoticeEvent.READ_ALL_MESSAGE));
        UdaUtils.hideInputMethod(this, view);
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if(count == 0) {
            showConfirm();
        } else {
            try {
                manager.popBackStackImmediate();
            } catch(IllegalStateException ignored) {
            }
        }
    }

    public void showConfirm(){
        EventBusUtil.post(new IndividualEvent(IndividualEvent.USER_INDIVIDUALITY_SIGNATURE));
        finish();
    }
}
